﻿var teamInitialMoney = 10000000;
var teamMoney = teamInitialMoney;
var teamPlayers = [];
showMoney();
showTeam();

function buyPlayer(player) {
    //TODO RP validaciones
    chargePlayer(player);
    addToTeam(player);
}

function chargePlayer(player) {
    teamMoney -= player.Price;
    showMoney();
}

function addToTeam(player) {
    teamPlayers.push(player);
    showTeam();
}

function showMoney() {
    var usedMoney = teamInitialMoney - teamMoney;
    $("#money-used").text(formatMoney(usedMoney));
    $("#money-remaining").text(formatMoney(teamMoney));
}

function showTeam() {
    var teamPlayersTable = $("#team-players");
    teamPlayersTable.empty();
    for (var i in teamPlayers) {
        var playerRow = createPlayerRow(teamPlayers[i]);
        teamPlayersTable.append(playerRow);
    }
}

function createPlayerRow(player) {
    var row = $("<tr>");
    row.append($("<td>").text(player.Position));
    row.append($("<td>").text(player.Name));
    row.append($("<td>").text(player.TeamName));
    row.append($("<td>").text(formatMoney(player.Price)).addClass("money"));
    return row;
}